//
//  HACAxisRenderer.m
//  Pods
//
//  Created by Hotacool on 2017/8/30.
//
//

#import "HACAxisRenderer.h"
#import "HACChartUtils.h"
#import "HACAxisProvider.h"
#import "HACBasicComponents.h"

@implementation HACAxisRenderer

- (void)drawInRect:(CGRect)rect context:(CGContextRef)context {
    NSArray *datas;
    if ([self.provider conformsToProtocol:@protocol(HACAxisProvider)]) {
        id<HACAxisProvider> provider = self.provider;
        if (self.axisType == DrawingAxisTypeX) {
            datas = [provider X_axisDatasInRect:rect];
        } else {
            datas = [provider Y_axisDatasInRect:rect];
        }
    }
    for (HACAxisComponents *axisItem in datas) {
        // 线
        CGContextSetLineDash (context, 0, 0, 0);
        if (axisItem.dashLengths) { // 如果需要虚线
            NSInteger count = [axisItem.dashLengths count];
            CGFloat lenghts[count];
            for (int i = 0; i < count; i ++)
            {
                lenghts[i]  = [[axisItem.dashLengths objectAtIndex:i] floatValue];
            }
            CGContextSetLineDash(context, 0, lenghts, count);
        }
        
        [HACChartUtils drawLine:context
                   pt1:axisItem.line.start
                   pt2:axisItem.line.end
                   clr:axisItem.lineColor
                 width:axisItem.lineBolder];
        
        // 文字
        NSString *textStr = axisItem.text;
        if (textStr) {
            [HACChartUtils drawStrInRect:textStr
                           rect:axisItem.textFrame
                           font:axisItem.textFont
                          color:axisItem.textColor
                      alignment:axisItem.textAlignment];
        }
        
        // 扩展文字
        textStr = axisItem.extText;
        if (textStr) {
            [HACChartUtils drawStrInRect:textStr
                           rect:axisItem.extTextFrame
                           font:axisItem.extTextFont
                          color:axisItem.extTextColor
                      alignment:axisItem.extTextAlignment];
        }
    }
}
@end
